package com.xiaoyy.app.extend.integration.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xiaoyy.app.extend.common.enums.AppApiEnum;
import com.xiaoyy.app.extend.common.vo.SrcmPersonVO;
import com.xiaoyy.app.extend.integration.RcmIntegration;
import com.xiaoyy.core.common.enums.EquipmentTypeEnum;
import com.xiaoyy.core.common.enums.LoginTypeEnum;
import com.xiaoyy.core.common.enums.UserTypeEnum;
import com.xiaoyy.core.common.exception.AppException;
import com.xiaoyy.core.common.vo.AppResponse;
import com.xiaoyy.core.common.vo.LoginVO;
import com.xiaoyy.core.common.vo.TokenVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class RcmIntegrationImpl implements RcmIntegration {
    @Value("${system.config.srcm.url}")
    private String srcmUrl;
    @Value("${system.config.jhrc.app.url}")
    private String appUrl;
    @Value("${system.config.srcm.secretKey}")
    private String secret_key;

    @Override
    public TokenVO checkToken(String token) {
        if (StringUtils.isBlank(token)) {
            throw new AppException("token不能为空");
        }
        String checkUrl = srcmUrl + "/checktoken?token=" + token + "&time=" + System.currentTimeMillis();
        String body = "";
        try {
            // 校验省人才码信息
            body = HttpUtil.get(checkUrl);

        } catch (Exception e) {
            throw new AppException("调用省人才码校验接口异常：" + e.getMessage());
        }
        if (StringUtils.isBlank(body)) {
            throw new AppException("省人才码未返回结果信息");
        }
        JSONObject jsonObject = null;
        try {
            jsonObject = JSON.parseObject(body);
        } catch (Exception e) {
            throw new AppException("省人才码返回信息异常：" + body);
        }
        Integer state = jsonObject.getInteger("state");
        if (state != null && state == 1) {
            //成功
            JSONObject data = jsonObject.getJSONObject("data");
            String loginBody = "";
            LoginVO loginVO = new LoginVO();
            loginVO.setEquipmentType(EquipmentTypeEnum.APP.getType());
            loginVO.setUserType(UserTypeEnum.USER.getType());
            loginVO.setFullName(data.getString("BH_PNAME"));
            loginVO.setIdcard(data.getString("BH_ID_NUM"));
            loginVO.setCountry(data.getString("BH_COUNTRY"));
            loginVO.setCellphone(data.getString("BH_PMO"));
            loginVO.setTalentTypeList(JSON.parseArray(data.getString("BH_LEVEL"), String.class));
            loginVO.setLoginType(LoginTypeEnum.srcm.getType());
            try {
                loginBody = HttpUtil.post(appUrl + AppApiEnum.RCMLOGIN0001.getType(), JSON.toJSONString(loginVO));
            } catch (Exception e) {
                throw new AppException("内部登录服务请求异常");
            }
            if (StringUtils.isNotBlank(loginBody)) {
                AppResponse appResponse = JSON.parseObject(loginBody, AppResponse.class);
                if (appResponse.getCode() == 0) {
                    Object obj = appResponse.getResult();
                    return JSON.parseObject(JSON.toJSONString(obj), TokenVO.class);
                } else {
                    if (StringUtils.isBlank(appResponse.getMessage())) {
                        throw new AppException(loginBody);
                    }
                    throw new AppException(appResponse.getMessage());
                }
            } else {
                throw new AppException("未返回内部登录数据");
            }

        } else {
            //失败
            String msg = jsonObject.getString("msg");
            throw new AppException("省人才码返回：" + msg);
        }
    }

    @Override
    public SrcmPersonVO getSrcmPerson(SrcmPersonVO vo) {
        if (StringUtils.isBlank(vo.getIdcard())) {
            throw new AppException("证件号码不能为空");
        }
        Long exAt = DateUtil.currentSeconds();
        String sign = SecureUtil.md5(vo.getIdcard() + secret_key + exAt);
        String url = srcmUrl + "/personinfo?" + "idNum=" + vo.getIdcard() + "&exAt=" + exAt + "&sign=" + sign;
        String jgBody = "";
        try {
            jgBody = HttpUtil.get(url);
        } catch (Exception e) {
            throw new AppException("调用省同步数据接口异常" + e.getMessage());
        }
        if (StringUtils.isNotBlank(jgBody)) {
            JSONObject jsonObject = null;
            try {
                jsonObject = JSON.parseObject(jgBody);
            } catch (Exception e) {
                throw new AppException("省人才码返回信息异常：" + jgBody);
            }
            Integer state = jsonObject.getInteger("state");
            if (state != null && state == 1) {
                //成功
                JSONObject data = jsonObject.getJSONObject("data");
                String loginBody = "";
                SrcmPersonVO srcmPersonVO = new SrcmPersonVO();

                srcmPersonVO.setFullName(data.getString("BH_PNAME"));
                srcmPersonVO.setIdcard(data.getString("BH_ID_NUM"));
                srcmPersonVO.setCountry(data.getString("BH_COUNTRY"));
                srcmPersonVO.setCellphone(data.getString("BH_PMO"));
                srcmPersonVO.setTalentTypeList(JSON.parseArray(data.getString("BH_LEVEL"), String.class));
                return srcmPersonVO;
            } else {
                //失败
                String msg = jsonObject.getString("msg");
                throw new AppException("省人才码返回：" + msg);
            }
        } else {
            throw new AppException("省里未返回结果数据");
        }
    }
}
